package com.sq.partner.manager.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;


import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sq.partner.manager.dao.IGatewayChannelAppDao;
import com.sq.partner.manager.dao.ITradNoCarProdConInsureDao;
import com.sq.partner.manager.dao.ITradNoCarProdDao;
import com.sq.partner.manager.dao.ITradNoCarProdPlanDao;
import com.sq.partner.manager.dao.ITradNoCarRiskProdDao;
import com.sq.partner.manager.mybatis.model.GatewayChannelApp;
import com.sq.partner.manager.mybatis.model.NocarConInsu;
import com.sq.partner.manager.mybatis.model.NocarPlan;
import com.sq.partner.manager.mybatis.model.NocarProd;
import com.sq.partner.manager.mybatis.model.NocarProdDefalut;
import com.sq.partner.manager.mybatis.model.NocarRisk;
import com.sq.partner.manager.mybatis.model.NocarRiskCal;
import com.sq.partner.manager.service.IGatewayChannelInfoConfigService;
import com.sq.partner.manager.service.IGatewayClearCacheManager;
import com.sq.partner.manager.service.IGatewayNoCarProductService;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.StrUtil;

import java.util.List;
import java.util.Map;
@Service
public class GatewayNoCarProductServiceImpl extends BaseServiceImpl<NocarProd, String> implements
IGatewayNoCarProductService {

	@Resource
	ITradNoCarProdDao tradNoCarProdInstDao;
	@Resource
	ITradNoCarRiskProdDao tradNoCarRiskProdDao;
	@Resource
	ITradNoCarProdPlanDao tradNoCarProdPlanDao;
	@Resource
	IGatewayClearCacheManager gatewayClearCacheManager;
	@Resource
	IGatewayChannelInfoConfigService gatewayChannelInfoConfigService;
	@Resource
	private IGatewayChannelAppDao gatewayChannelAppDao;
	@Resource
	private ITradNoCarProdConInsureDao tradNoCarProdConInsureDao;
	
	@Override
	public Page<NocarProd> queryPage(Page<NocarProd> page) throws SQLException {
		return tradNoCarProdInstDao.queryPage(page);
	}
	@Override
	public NocarProd saveNoCarTradProdInst(NocarProd noCarProd) throws Exception {
		NocarProd temnocarProd =tradNoCarProdInstDao.findMaxNocarTradProdCode(noCarProd);
		if(null!=temnocarProd&&StrUtil.isNotEmpty(temnocarProd.getProdCode())){
			long prodCode= Long.parseLong(temnocarProd.getProdCode().substring(4))+1;
			String nextProdCodeVal = noCarProd.getProdType().concat(String.format("%05d", prodCode));
			noCarProd.setProdCode(nextProdCodeVal);
		}else{
			noCarProd.setProdCode(noCarProd.getProdType()+"00001");
		}
		Date sysdate = new Date();
		noCarProd.setInsertTime(sysdate);
		tradNoCarProdInstDao.saveNoCarTradProd(noCarProd);
		//新增产品,清交易系统缓存
		cleanTradSystemCache(noCarProd.getProdCode());
		return noCarProd;
	}
	@Override
	public NocarProd queryNocarTradProdInstById(String tradProdInst)
			throws SQLException {
		return tradNoCarProdInstDao.queryNocarTradProdInstById(tradProdInst);
	}
	

	@Override
	public Page<NocarRisk> queryRiskPlanBasePage(Page<NocarRisk> page,
			String prodCode) throws SQLException {
		return tradNoCarRiskProdDao.queryRiskPlanBasePage(page, prodCode);
	}
	@Override
	public void saveRisk(NocarRisk noCarRisk) throws Exception {
			tradNoCarRiskProdDao.saveNocarRisk(noCarRisk);
			//险别有变化清交易系统缓存
			cleanTradSystemCache(noCarRisk.getProdCode());
	}
	@Override
	public void updateRisk(NocarRisk noCarRisk) throws Exception {
		tradNoCarRiskProdDao.updateRisk(noCarRisk);
		//险别有变化清交易系统缓存
		cleanTradSystemCache(noCarRisk.getProdCode());
		
	}
	@Override
	public void deleteRisk(String prodCode, String riskCode ,Long nField1) throws Exception {
		NocarRisk tem = new NocarRisk();
		tem.setProdCode(prodCode);
		tem.setRiskCode(riskCode);
		tem.setnField1(nField1);
		tradNoCarRiskProdDao.deleteRisk(tem);
		//险别有变化清交易系统缓存
		cleanTradSystemCache(prodCode);
		
	}
	@Override
	public void deleteRiskByProdCode(String prodCode)throws Exception 
	{
		tradNoCarRiskProdDao.deleteRiskByProdCode(prodCode);
		//险别有变化清交易系统缓存
		cleanTradSystemCache(prodCode);
	}
	@Override
	public void updateNocarProd(NocarProd noCarProd)throws Exception 
	{
		tradNoCarProdInstDao.updateNocarProd(noCarProd);
		//编辑产品时判断产品是否有合作伙伴应用关联方案，如果有就清除缓存
		cleanTradSystemCache(noCarProd.getProdCode());

	}
	@Override
	public void deleteNocarProd(String prodCode)throws Exception 
	{
		tradNoCarProdInstDao.deleteNocarProd(prodCode);
		//编辑产品时判断产品是否有合作伙伴应用关联方案，如果有就清除缓存
		cleanTradSystemCache(prodCode);
	}
	@Override
	public boolean isExistRisk(String prodCode,String riskCode) throws SQLException
	{
		return tradNoCarRiskProdDao.isExistRisk(prodCode, riskCode);
	}
	@Override
	public boolean isExistProdPlan(String prodCode) throws SQLException
	{
		return tradNoCarProdInstDao.isExistProdPlan(prodCode);
	}
	@Override
	public NocarRisk  findMaxRiskProdIndex(NocarRisk nocarRisk) throws SQLException {
		return tradNoCarRiskProdDao.findMaxNocarRiskCode(nocarRisk);
	}
	
	@Override
	public int  queryProdRiskCountByCode(String prodCode) throws SQLException {
		return tradNoCarRiskProdDao.queryProdRiskCountByCode(prodCode);
	}
	
	
	private void  cleanTradSystemCache(String prodCode) throws Exception
	{
		List<NocarPlan> tradNocarPlanLists= tradNoCarProdPlanDao.queryTradNocarPlanByProdCode(prodCode);
		if (tradNocarPlanLists != null && tradNocarPlanLists.size() > 0) {
			//检测tradInsuPlanLists 大小以便逻辑判断
			if(tradNocarPlanLists.size()>1){
				List<String> beClearls = new ArrayList<String>();
				for(int i=0;i<tradNocarPlanLists.size();i++){
					List<GatewayChannelApp> appLs = gatewayChannelAppDao.queryChannelAppInfoListByChannelCode(tradNocarPlanLists.get(i).getPartnerCode());
					//对于集合中的第一个合作伙伴操作
					if(null != appLs && appLs.size() > 0 && 0==i){
						for (int j = 0;j<appLs.size();j++) {
								gatewayClearCacheManager.clearAllCache(appLs.get(j).getAppId());
								beClearls.add(appLs.get(j).getAppId());
							}
					}else if(null != appLs && appLs.size() > 0 && i>0){
						for (int j = 0;j<appLs.size();j++) {
							if(!beClearls.contains(appLs.get(j).getAppId())){
								gatewayClearCacheManager.clearAllCache(appLs.get(j).getAppId());
								beClearls.add(appLs.get(j).getAppId());
							}
						}
					}
				}
				beClearls=null;
			} else {
				List<GatewayChannelApp> appLs = gatewayChannelAppDao.queryChannelAppInfoListByChannelCode(tradNocarPlanLists.get(0).getPartnerCode());
				if (null != appLs && appLs.size() > 0) {
					for (GatewayChannelApp tem : appLs) {
						gatewayClearCacheManager.clearAllCache(tem.getAppId());
					}
				}
			}
		}
	}
	@Override
	public Page<NocarProd> queryPageForNocarInsure(Page<NocarProd> page) throws SQLException {
		return tradNoCarProdInstDao.queryPageForNocarInsure(page);
	}
	@Override
	public Page<NocarConInsu> queryProdConInsureInfoPage(Page<NocarConInsu> page, String prodCode) throws SQLException {
		return tradNoCarProdConInsureDao.queryProdConInsureInfoPage(page, prodCode);
	}
	@Override
	public boolean saveProdConInsure(NocarConInsu noCarConInsu) throws Exception {
		boolean resultFlag = false;
		resultFlag = tradNoCarProdConInsureDao.saveProdConInsure(noCarConInsu);
		if(true == resultFlag){
			cleanTradSystemCache(noCarConInsu.getPRODCODE());
		}
		return resultFlag;
	}
	@Override
	public boolean deleteProdConInsureByProdCode(String prodCode) throws Exception {
		boolean resultFlag = false;
		resultFlag = tradNoCarProdConInsureDao.deleteProdConInsureByProdCode(prodCode);
		if(true == resultFlag){
			cleanTradSystemCache(prodCode);
		}
		return resultFlag;
	}
	@Override
	public boolean searchProdConInsureInfoByProdCode(String prodCode) throws Exception {
		return tradNoCarProdConInsureDao.searchProdConInsureInfoByProdCode(prodCode);
	}

	@Override
	public boolean findNocarByMap(Map<String, Object> map) throws SQLException {
		List<NocarRisk>  risks=tradNoCarRiskProdDao.findNocarByMap(map);
		if(risks==null||risks.size()<1){
			return false;
		}
		return true;
	}

	
	@Override
	public  List<Map<String, Object>> searchNoCarProdsByprodCode(Map<String, Object> map)
			throws Exception {
		return  tradNoCarProdInstDao.searchNoCarProdsByprodCode(map);
	}
	@Override
	public List<Map<String, Object>> searchNoCarRisksByprodCode(Map<String, Object> map)
			throws Exception {
		return tradNoCarRiskProdDao.searchNoCarRisksByprodCode(map);
	}
	@Override
	public List<Map<String, Object>> searchNoCarRiskCalsByprodCode(Map<String, Object> map)
			throws Exception {
		// TODO Auto-generated method stub
		return tradNoCarRiskProdDao.searchNoCarRiskCalsByprodCode(map);
	}
	@Override
	public  List<Map<String, Object>> searchNoCarProdDefaultsByprodCode(
			Map<String, Object> map) throws Exception {
		return tradNoCarRiskProdDao.searchNoCarProdDefaultsByprodCode(map);
	}

}
